﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Net.Mail;
using ESN.Models;
using ESN.Helpers;
using System.Net;
using System.IO;

namespace ESN.Controllers
{
    public class TestController : Controller
    {
        //
        // GET: /Test/
        private ESNCoreDbContext core = new ESNCoreDbContext();
        private ESNQuizDbContext quiz = new ESNQuizDbContext();

        public string Index()
        {
            return Membership.GetUser().ProviderUserKey.ToString();
        }

        public string Core()
        {
            App a = new App();
            a.AppPublic = true;
            a.AppUrl = "/Quiz/";
            a.AppName = "Quiz";
            a.AppFrame = false;
            a.AppKey = "123qwe";

            core.Apps.Add(a);
            App a2 = new App();
            a2.AppPublic = true;
            a2.AppUrl = "/Canvas/";
            a2.AppName = "Canvas";
            a2.AppFrame = false;
            a2.AppKey = "qwe123";

            core.Apps.Add(a2);

            core.SaveChanges();

            return "ok";
        }

        public string Quiz()
        {
            Category c1 = new Category();
            c1.CatName = "IT";
            c1.CatPublic = true;
            c1.CatTime = DateTime.Now;
            quiz.Categories.Add(c1);

            Category c2 = new Category();
            c2.CatName = "Sinh Học";
            c2.CatPublic = true;
            c2.CatTime = DateTime.Now;
            quiz.Categories.Add(c2);

            Category c3 = new Category();
            c3.CatName = "OOP";
            c3.ParentId = 1;
            c3.CatPublic = true;
            c3.CatTime = DateTime.Now;
            quiz.Categories.Add(c3);

            Category c4 = new Category();
            c4.CatName = "Thực vật học";
            c4.ParentId = 2;
            c4.CatPublic = true;
            c4.CatTime = DateTime.Now;
            quiz.Categories.Add(c4);

            Category c5 = new Category();
            c5.CatName = "Động vật học";
            c5.ParentId = 2;
            c5.CatPublic = true;
            c5.CatTime = DateTime.Now;
            quiz.Categories.Add(c5);

            quiz.SaveChanges();
            return "ok";

        }

        public void UserRoles()
        {
            if (!Roles.RoleExists("Admin"))
                Roles.CreateRole("Admin");
            if (!Roles.RoleExists("SMod"))
                Roles.CreateRole("SMod");
            if (!Roles.RoleExists("Mod"))
                Roles.CreateRole("Mod");

            Roles.AddUserToRole("nvcnvn", "Admin");
        }

        public string MyInfo()
        {
            return PermissionHelper.readAllExercises().ToString() + " " + PermissionHelper.isAdmin().ToString() + " " + Roles.IsUserInRole("Admin").ToString();
        }

        public string Mail()
        {
            MailMessage message = new MailMessage();
            message.From = new MailAddress("mail@esn.thi.vn");

            message.To.Add(new MailAddress("nvcnvn1@gmail.com"));

            message.Subject = "This is my subject";
            message.Body = "This is the content";

            SmtpClient client = new SmtpClient();
            client.EnableSsl = false;
            client.Send(message);
            return "ok";
        }
        public ActionResult Remote()
        {
            Object o = new Object();
            return View();
        }
        [HttpPost]
        public string Remote(HttpPostedFileBase file)
        {
            var byteArray = new byte[file.ContentLength];
            file.InputStream.Read(byteArray, 0, file.ContentLength);
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("https://apis.live.net/v5.0/me/skydrive/files/Avatar-" + Request.Form["UserId"] + file.FileName + "?access_token=" + core.AppToken.Find(1).access_token);
            // Set the Method property of the request to POST.
            request.Method = "PUT";

            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
            string responseFromServer = "";
            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    Stream dataStream = response.GetResponseStream();

                    StreamReader reader = new StreamReader(dataStream);
                    responseFromServer = reader.ReadToEnd();
                }
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    //Console.WriteLine("Error code: {0}", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        string text = new StreamReader(data).ReadToEnd();
                        return httpResponse.StatusCode.ToString() + text;
                    }
                }
            }
            return responseFromServer;
        }

        public ActionResult WebSocket()
        {
            return View();
        }
    }
}
